Skip to content

--categorical reads numeric-looking labels as text (#124) - #125

Merged
Claptar merged 3 commits into
mainfrom
fix/124-categorical-labels
Nov 26, 2026
Merged

Claptar merged 3 commits into
mainfrom
fix/124-categorical-labels

Conversation

@Claptar

@Claptar Claptar commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #124.

adata import dataframe … --categorical leiden used to keep a column of numeric-looking labels ("0", "1", "10") as int64 and print only a note. So an export dataframe → edit → import dataframe round trip lost the most common obs categoricals.

A column named in --categorical is now always read as text and stored as a categorical, whatever its values look like:

  • leiden's 0, 1, 10 stay labels, with categories in anndata's natural order (0, 1, 2, 10), and 007 keeps its zeros;
  • numbers and True/False keep the text they were written as, and what pandas reads as missing stays a gap. This matches pd.read_csv(dtype=str) followed by strings_to_categoricals, which the new tests use as the oracle;
  • past the cap of distinct values (65,536), a forced column is written as strings, numbers included, as the existing warning already said.

There is no change to formats/table.py: the scan already keeps every column's distinct values as written.

Not in this PR. Both are noted in STATUS.md under follow-ups:

  • --keep-dtypes, to keep the existing frame's encodings (category order, ordered, dtypes);
  • an import that replaces only the CSV's columns.

Changelog entry (for 0.17.7)

Fixed

  • --categorical keeps numeric-looking labels (#124).
    adata import dataframe … --categorical leiden used to leave leiden clusters 0, 1, 10 as int64. Columns named in --categorical are now read as text and stored as categoricals whatever their values, so an export → edit → import round trip keeps them, and 007 keeps its zeros.

Tests

  • test_numeric_labels_named_categorical_survive_a_round_trip (h5ad, zarr2, zarr3) is the issue's round trip, with a "007"/"7" column and a gap. It fails on main.
  • test_categorical_overrides_still_apply now forces a float column, a whole-number column with a gap and a True/False column, and checks them against the oracle.
  • test_a_forced_numeric_column_past_the_cap_is_written_as_strings.
  • Full unit suite: 4015 passed, 9 skipped. The issue's reproduction now gives leiden: category with categories ['0', '1', '2', '10'].

🤖 Generated with Claude Code

A column named in --categorical whose values looked like numbers (leiden's
"0", "1", "10") was kept as int64 with only a note, so an export, edit and
import of obs lost the clusters' labels, and "007" its zeros. A forced
column is now always read as text and stored as a categorical: numbers and
True/False keep the text they were written as, and what pandas reads as
missing stays a gap, matching pd.read_csv(dtype=str) and anndata's
strings_to_categoricals. Past the cap of distinct values it is written as
strings, numbers included, as the warning already said.

--keep-dtypes and a columns-only import, also raised in #124, are noted in
STATUS.md as follow-ups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 16:21
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T16:22:24.920354Z 00de393 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Test Results (py3.13)

3 817 tests   3 805 ✅  5m 26s ⏱️
    1 suites     12 💤
    1 files        0 ❌

Results for commit 76e414a.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Test Results (py3.12)

3 817 tests   3 805 ✅  10m 4s ⏱️
    1 suites     12 💤
    1 files        0 ❌

Results for commit 76e414a.

♻️ This comment has been updated with latest results.

Claptar and others added 2 commits November 26, 2026 16:42
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
STATUS.md keeps both follow-ups: #124's and the GitHub releases note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Claptar
Claptar merged commit 85723b6 into main Nov 26, 2026
11 checks passed
@Claptar Claptar mentioned this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

import dataframe: --categorical does not keep digit-only labels categorical (round trip turns them into int64)

2 participants